class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int ans = 0;
        int mini = 1000000, size = prices.size();
        for (int i = 0; i < size; i++) {
            if (mini <= prices[i]) {
                continue;
            }
            else {
                mini = prices[i];
            }
            for (int j = i + 1; j < size; j++) {
                if (prices[j] - prices[i] > ans) {
                    ans = prices[j] - prices[i];
                }
            }
        }
        return ans;
    }
};

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int ans = 0;
        int min = 1000000;
        for (auto x : prices) {
            if (min > x) {
                min = x;
            }
            else if (x - min > ans) {
                ans = x - min;
            }
        }
        return ans;
    }
};